from django.db import models


class GoodsCategory(models.Model):
    """商品类别"""
    name = models.CharField(max_length=10, verbose_name='名称')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'tb_goods_category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Brand(models.Model):
    """品牌"""
    name = models.CharField(max_length=20, verbose_name='名称')
    logo = models.ImageField(verbose_name='Logo图片', upload_to='images')
    first_letter = models.CharField(max_length=1, verbose_name='品牌首字母')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'tb_brand'

    verbose_name = '品牌'
    verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# Guige_leixing
class Guige_leixing(models.Model):
    # 服饰 白色，黑色
    # 电子产品 128g 256g
    name = models.CharField(max_length=100, verbose_name='规格类型')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return str(self.name)

    class Meta:
        verbose_name = '商品规格类型'
        verbose_name_plural = verbose_name


# Guige
class Guige(models.Model):
    name = models.CharField(max_length=100, verbose_name='规格')
    guige_leixing = models.ForeignKey(Guige_leixing, verbose_name='规格类型id', null=True, on_delete=models.PROTECT)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return str(self.name)

    class Meta:
        verbose_name = '商品规格'
        verbose_name_plural = verbose_name


class shangpin(models.Model):
    """商品"""
    name = models.CharField(max_length=50, verbose_name='名称')
    caption = models.CharField(max_length=100, verbose_name='副标题')
    # ForeignKey 一对多关系，一个商品可对应多个可选类别
    category = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, verbose_name='从属类别')
    # 一个商品可对应多个可选品牌
    brand = models.ForeignKey(Brand, on_delete=models.PROTECT, verbose_name='品牌')
    # guige_leixing = models.ForeignKey(Guige_leixing,on_delete=models.PROTECT, verbose_name='规格类型', null=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')  # decimal_places 存储十进位数
    stock = models.IntegerField(default=0, verbose_name='库存')
    sales = models.IntegerField(default=0, verbose_name='销量')
    comments = models.IntegerField(default=0, verbose_name='评价数')
    # upload_to = 'images' 指定相对路径
    image = models.ImageField(max_length=200, verbose_name='展示图片', upload_to='images')
    guige_leixing = models.ForeignKey(Guige_leixing, on_delete=models.PROTECT, verbose_name='规格类型', null=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'tb_shangpin'
        verbose_name = '商品信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.id, self.name)


class CartInfos(models.Model):
    num = models.IntegerField(verbose_name='购买数量')
    user_id = models.IntegerField('用户ID')
    shangpin_id = models.IntegerField('商品ID')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间", null=True)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间", null=True)

    def __str__(self):
        return str(self.id)

    class Meta:
        verbose_name = '购物车'
        verbose_name_plural = '购物车'
        db_table = 'tb_cartinfo'


STATE = (
    ('待支付', '待支付'),
    ('已支付', '已支付'),
    ('发货中', '发货中'),
    ('已签收', '已签收'),
    ('退货中', '退货中'),
)


class OrderInfos(models.Model):
    id = models.AutoField(primary_key=True)
    price = models.FloatField('订单总价')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间", null=True)
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间", null=True)
    user_id = models.IntegerField('用户ID')
    state = models.CharField('订单状态', max_length=20, choices=STATE)

    def __str__(self):
        return str(self.id)

    class Meta:
        verbose_name = '订单信息'
        verbose_name_plural = '订单信息'
